<?php
// 只能在当前 request 生命周期缓存，要跨进程，可以再加一层缓存： memcached/xcache/apc/
$g_static_users = array(); // 变量缓存

// hook model_user_start.php

// ------------> 最原生的 CURD，无关联其他数据。

function sort__create($arr) {
	// hook model_sort__create_start.php

	$r = db_create('sort', $arr);
	// hook model_sort__create_end.php
	return $r;

}

function sort__update($sid, $update) {
	// hook model_sort__update_start.php
	$r = db_update('sort', array('sid' => $sid), $update);
	// hook model_sort__update_end.php
	return $r;
}

function sort__read($sid) {
	// hook model_sort__read_start.php
	$sort = db_find_one('sort', array('sid' => $sid));
	// hook model_sort__read_end.php
	return $sort;
}

function sort__delete($sid) {
	// hook model_sort__delete_start.php
	$r = db_delete('sort', array('sid' => $sid));
	// hook model_sort__delete_end.php
	return $r;
}

function sort__find($cond = array(), $orderby = array(), $page = 1, $pagesize = 1000) {
	// hook model_sort__find_start.php
	$sortlist = db_find('sort', $cond, $orderby, $page, $pagesize, 'sid');
	// hook model_sort__find_end.php
	return $sortlist;
}
// ------------> 关联 CURD，主要是强相关的数据，比如缓存。弱相关的大量数据需要另外处理。

// 新建分类

function sort_create($arr) {
	// hook model_sort_create_start.php

	$r = sort__create($arr);
	sort_list_cache_delete();
	// hook model_sort_create_end.php
	return $r;
}

// 更新分类
function sort_update($sid, $update) {
	$r = sort__update($sid, $update);
	sort_list_cache_delete();
	return $r;
}

// 查看单个分类
function sort_read($sid) {
	$sort = sort__read($sid);
	return $sort;
}

// 查看板块分类
function sort_find_by_fid($fid) {
	$sortlist = sort__find(array('fid' => $fid));
	return $sortlist;
}

function sort_find_by_fid_cache($fid) {
	global $conf;
	$sortlist = cache_get('sortlist-' . $fid);

	// hook model_sort_list_cache_start.php

	if ($sortlist === NULL) {
		$sortlist = sort_find_by_fid($fid);
		cache_set('sortlist-' . $fid, $sortlist, 60);
	}
	// hook model_sort_list_cache_end.php
	return $sortlist;
}

// 查看所有分类
function sort_find($cond = array(), $orderby = array(), $page = 1, $pagesize = 1000) {
	// hook model_sort_find_start.php
	$sortlist = sort__find($cond, $orderby, $page, $pagesize);
	// hook model_sort_find_end.php
	return $sortlist;
}

// 关联数据删除
function sort_delete($sid) {
	$r = sort__delete($sid);

	sort_list_cache_delete();
	// hook model_sort_delete_end.php
	return $r;
}

//其他方法

// 从缓存中读取 sort_list 数据x
function sort_list_cache() {
	global $conf, $sortlist;
	$sortlist = cache_get('sortlist');

	// hook model_sort_list_cache_start.php

	if ($sortlist === NULL) {
		$sortlist = sort_find();
		cache_set('sortlist', $sortlist, 60);
	}
	// hook model_sort_list_cache_end.php
	return $sortlist;
}

//更新缓存
function sort_list_cache_delete() {
	global $conf;
	static $deleted = FALSE;
	if ($deleted) {
		return;
	}

	// hook model_sort_list_cache_delete_start.php

	cache_delete('sortlist');
	$deleted = TRUE;
	// hook model_sort_list_cache_delete_end.php
}

// 板块和分类序列表
function sort_forum_list() {
	$sortlist = sort_list_cache();
	$forumlist = forum_list_cache();

	$list = array();

	foreach ($forumlist as $_forum) {
		$forum = $_forum;
		//$forum['sortlist'] = arrlist_cond_orderby($sortlist, array('fid' => $_forum['fid']));
		$slist = arrlist_cond_orderby($sortlist, array('fid' => $_forum['fid']));
		if ($slist != array()) {
			foreach ($slist as $sort) {
				$forum['sortlist'][] = $sort;
			}
		} else {
			$forum['sortlist'] = array();
		}
		$list[] = $forum;
	}

	return $list;

}
//从缓存中获得序列表
function sort_forum_list_cache() {
	global $conf, $sortlist;
	$sortforumlist = cache_get('sortforumlist');

	// hook model_sort_forum_list_cache_start.php

	if ($sortforumlist === NULL) {
		$sortforumlist = sort_forum_list();
		cache_set('sortforumlist', $sortforumlist, 60);
	}
	// hook model_sort_forum_list_cache_end.php
	return $sortforumlist;
}

function sort_forum_list_cache_delete() {
	global $conf;
	static $deleted = FALSE;
	if ($deleted) {
		return;
	}

	// hook model_sort_list_cache_delete_start.php

	cache_delete('sortforumlist');
	$deleted = TRUE;
	// hook model_sort_forum_list_cache_delete_end.php
}

function sort_maxid() {
	// hook model_sort_maxid_start.php
	$n = db_maxid('sort', 'sid');
	// hook model_sort_maxid_end.php
	return $n;
}

?>